package org.seedframework.sharding.mybatis;

import lombok.extern.slf4j.Slf4j;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;


/**
 * route database and table name
 */
@Slf4j
public class ScatterRoutingSupport {

    /**
     * data source locate
     */
    private static InheritableThreadLocal<DataSource> dataSource = new InheritableThreadLocal<>();

    /**
     * table name locate
     */
    private static InheritableThreadLocal<String> tableNameSuffix = new InheritableThreadLocal<>();


    /**
     * DAO method and handle strategy
     */
    private static Map<String, ScatterRoutingDefinition> methodStrategyMap = new HashMap<>();


    public static void setDataSource(DataSource ds) {
        dataSource.set(ds);
    }

    public static void setTableNameSuffix(String suffix) {
        tableNameSuffix.set(suffix);
    }


    public static DataSource getDataSource() {
        return dataSource.get();
    }

    public static String getTableNameSuffix() {
        return tableNameSuffix.get();
    }


    public static void methodUseScatter(String methodFullPath, ScatterRoutingDefinition definition) {
        methodStrategyMap.put(methodFullPath, definition);
    }

    public static ScatterRoutingDefinition getMethodScatterStrategy(String methodFullPath) {
        return methodStrategyMap.get(methodFullPath);
    }



}
